import Axios from "axios";
import Cookies from 'js-cookie';
import { AppPopupModal, AppPopupLoading, AppPopupRemove } from "../page/layout/popup";

function http(method, url, params, data, throwError) {
    const origin = 'https://api.haitunxueche.com/';
    let headers = {};
    const token = Cookies.get('sale');
    if (token) {
        headers['Authorization'] = token;
    }
    return new Promise((resl, rej) => {
        AppPopupLoading();
        Axios({
            method: method,
            baseURL: origin,
            url: url,
            headers: headers,
            params: params,
            data: data,
            timeout: 1000,            
        })
            .then(d => {
                AppPopupRemove();
                resl(d);
            })
            .catch(
                e => {
                    AppPopupRemove();
                    if (throwError) {
                        throw (e);
                    } else {
                        console.warn(e);
                        AppPopupModal(JSON.stringify(e));
                    }
                }
            );
    })

}

export default http;